import 'dart:io';

import 'package:bxultimate/app/router/router_name.dart';
import 'package:bxultimate/app/theme/gaps.dart';
import 'package:bxultimate/resources/assets.dart';
import 'package:bxultimate/shared/constant/app_constant.dart';
import 'package:bxultimate/shared/util/util_sp.dart';
import 'package:bxultimate/widgets/mk_load_image.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';

import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

class SplashPage extends StatefulWidget {
  const SplashPage({super.key});

  @override
  _SplashPageState createState() => _SplashPageState();
}

class _SplashPageState extends State<SplashPage> {
  @override
  void initState() {
    super.initState();

    getData();
  }

  Future<void> getData({flag = true}) async {
    if (flag) {
      await Future.delayed(const Duration(milliseconds: 500));
    }

    showGeneralDialog(
      context: context,
      pageBuilder: (BuildContext buildContext, Animation<double> animation,
          Animation<double> secondaryAnimation) {
        return TDAlertDialog(
          title: '欢迎使用兵馨',
          contentWidget: _buildDialogContent(),
          leftBtn: TDDialogButtonOptions(
              title: '不同意',
              action: () {
                Get.back();
                showDisagreeDialog();
              }),
          rightBtn: TDDialogButtonOptions(
              title: '同意',
              action: () {
                _goHome();
              },
              theme: TDButtonTheme.primary),
        );
      },
    );
  }

  Widget _buildDialogContent() {
    return Padding(
      padding: EdgeInsets.only(top: 10),
      child: Column(
        children: [
          Text.rich(TextSpan(children: [
            TextSpan(text: "本个人信息保护指引将通过"),
            TextSpan(
                text: "用户协议",
                style: TextStyle(color: Colors.blue),
                recognizer: TapGestureRecognizer()
                  ..onTap = () {
                    Get.toNamed(RouterName.userSystemSettingUserAgreement);
                  }),
            TextSpan(text: "与"),
            TextSpan(
                text: "隐私政策",
                style: TextStyle(color: Colors.blue),
                recognizer: TapGestureRecognizer()
                  ..onTap = () {
                    Get.toNamed(RouterName.userSystemSettingPrivacy);
                  }),
            TextSpan(
              text: "帮助你了解我们如何手机、处理个人信息。",
            ),
          ])),
          Text(
            "1. 我们可能会申请位置权限，用户帮助你在发布的信息中展示位置或丰富信息推荐维度，城市/县区无需使用位置权限，仅通过ip地址确定城市及相关信息，不会收集精确位置信息",
          ),
          Text(
              '2. 上述权限以及摄像头、麦克风、相册（存储）、GPS、等权限均不会默认或者强制开启收集信息。你有权拒绝开启，拒绝授权后不会影响App提供基础功能服务'),
          Text('3. 为实现信息分享、第三方登录可能会调用剪切板并使用功能相关的最小必要信息（口令、链接等）')
        ],
      ),
    );
  }

  void showDisagreeDialog() {
    showGeneralDialog(
      context: context,
      pageBuilder: (BuildContext buildContext, Animation<double> animation,
          Animation<double> secondaryAnimation) {
        return TDConfirmDialog(
            title: '温馨提示',
            buttonText: '同意并继续',
            contentWidget: Padding(
                padding: EdgeInsets.only(top: 10),
                child: Column(children: [
                  Text.rich(
                    TextSpan(
                      children: [
                        TextSpan(text: "如果你不同意"),
                        TextSpan(
                            text: "个人信息保护指引",
                            style: TextStyle(color: Colors.blue),
                            recognizer: TapGestureRecognizer()
                              ..onTap = () {
                                Get.back();
                                getData(flag: false);
                              }),
                        TextSpan(
                            text: "，我们将无法为你提供兵馨App完整功能，你可以选择使用基本功能模式或直接退出应用"),
                      ],
                    ),
                  ),
                  TextButton(
                      onPressed: () {
                        exit(0);
                      },
                      child: Text(
                        // '仅使用基本功能模式',
                        '退出兵馨',
                        style: TextStyle(color: Colors.grey),
                      ))
                ])));
      },
    );
  }

  @override
  void dispose() {
    super.dispose();
  }

  void _goHome() {
    UtilSp.putBool(AppConstant.isFirstOpen, false);
    Get.offAllNamed(RouterName.main);
  }

  @override
  Widget build(BuildContext context) {
    return Material(
        color: Get.theme.scaffoldBackgroundColor,
        child: Stack(
          alignment: Alignment.center,
          children: [
            Positioned(
                bottom: 80,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    MkLoadImage(
                      Assets.imagesIcon,
                      width: 30,
                    ),
                    Gaps.hGap8,
                    Text(
                      '兵馨',
                      style:
                          TextStyle(fontSize: 18, fontWeight: FontWeight.w500),
                    )
                  ],
                )),
          ],
        ));
  }
}
